import java.util.Scanner;
import java.util.Random;

public class Exercise4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        
        // 生成1-100的随机数
        int targetNumber = random.nextInt(100) + 1;
        int maxAttempts = 5;
        int attempts = 0;
        boolean guessed = false;
        
        System.out.println("欢迎来到猜数字游戏！");
        System.out.println("我已经想好了一个1-100之间的数字，你有" + maxAttempts + "次机会来猜它。");
        
        while (attempts < maxAttempts && !guessed) {
            attempts++;
            System.out.println("\n第" + attempts + "次猜测，请输入你的猜测（1-100）：");
            
            int guess = scanner.nextInt();
            
            if (guess < 1 || guess > 100) {
                System.out.println("请输入1-100之间的数字！");
                attempts--; // 这次不算有效尝试
                continue;
            }
            
            if (guess == targetNumber) {
                System.out.println("🎉 恭喜你！猜对了！");
                System.out.println("你用了" + attempts + "次就猜对了！");
                guessed = true;
            } else if (guess < targetNumber) {
                System.out.println("太小了！");
                if (attempts < maxAttempts) {
                    System.out.println("还有" + (maxAttempts - attempts) + "次机会");
                }
            } else {
                System.out.println("太大了！");
                if (attempts < maxAttempts) {
                    System.out.println("还有" + (maxAttempts - attempts) + "次机会");
                }
            }
        }
        
        if (!guessed) {
            System.out.println("\n游戏结束！你没有在" + maxAttempts + "次内猜对。");
            System.out.println("正确答案是：" + targetNumber);
        }
        
        scanner.close();
    }
}
